/*
 * Lab5G.cpp
 * Description: Simple program to see how dynamic memory allocation
 *              works in a linked structure.
 *
 *  Created on: Feb 27, 2014
 *      Author: Anthony Varghese
 */

#include <string>

struct ListNode{
	std::string item;
	int count;
	ListNode* link;
};

#include <iostream>
#include <fstream>

int main(){
	char filename[] = "src/inputdata";
	std::cout << " Reading from file: " << filename << " . . . ";

	std::ifstream in( filename );
	if (in.fail()){
		std::cout << " ... not able to read it! Exiting." << std::endl;
		return -1;
	}
	std::cout << " ... OK, file opened." << std::endl << std::endl;

	ListNode* first = new ListNode();
	ListNode* p = first;
	int count = 1;

	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p->link = new ListNode();
	p = p->link;
	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p->link = new ListNode();
	p = p->link;
	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p = first;
	std::cout << p->count << ": " << p->item << std::endl;
	p = p->link;
	std::cout << p->count << ": " << p->item << std::endl;
	p = p->link;
	std::cout << p->count << ": " << p->item << std::endl;
}
